package com.jielihaofeng.chat.controller;

import com.jielihaofeng.chat.entity.ChatGroupUserEntity;
import com.jielihaofeng.chat.query.ChatGroupUserQuery;
import com.jielihaofeng.chat.service.ChatGroupUserService;
import com.jielihaofeng.chat.vo.ChatGroupUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.utils.Result;
import com.jielihaofeng.chat.convert.ChatGroupUserConvert;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 群组用户表
*
* @author 借力好风 949394048@qq.com
* @since 1.0.0 2022-07-22
*/
@RestController
@RequestMapping("chat/chatgroupuser")
@Tag(name="群组用户表")
@AllArgsConstructor
public class ChatGroupUserController {
    private final ChatGroupUserService chatGroupUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('chat:chatgroupuser:page')")
    public Result<PageResult<ChatGroupUserVO>> page(@Valid ChatGroupUserQuery query){
        PageResult<ChatGroupUserVO> page = chatGroupUserService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('chat:chatgroupuser:info')")
    public Result<ChatGroupUserVO> get(@PathVariable("id") Long id){
        ChatGroupUserEntity entity = chatGroupUserService.getById(id);

        return Result.ok(ChatGroupUserConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('chat:chatgroupuser:save')")
    public Result<String> save(@RequestBody ChatGroupUserVO vo){
        chatGroupUserService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('chat:chatgroupuser:update')")
    public Result<String> update(@RequestBody @Valid ChatGroupUserVO vo){
        chatGroupUserService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('chat:chatgroupuser:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        chatGroupUserService.delete(idList);

        return Result.ok();
    }
}